import requests
from lxml import etree
import csv

csv_headers = ['产品名称', '价格', '月售量', '评论']
g = open('./aj淘宝数据.csv', 'w', encoding='utf-8')
g_csv = csv.writer(g)
g_csv.writerow(csv_headers)
url = 'https://list.tmall.com/search_product.htm?q=aj&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
    'Cookie': 'cna=0UcFGLVL2D8CAXLvNDNdHDyt; hng=CN%7Czh-CN%7CCNY%7C156; _med=dw:1536&dh:864&pw:1920&ph:1080&ist:0; cq=ccp%3D1; tk_trace=1; xlly_s=1; sgcookie=E1004uIVy6hkNNahDno1JiFTNAoHAcA1JmiSF158mGfxC%2B1MO7jDbAqjGVh46O1FVitBW3ha7RScbdTxPb95agvU7g%3D%3D; t=4f306284b38c7e36d3e90eebb1ae44a2; _tb_token_=59e83ebee4b73; cookie2=1ddfe3e4813aa0e474f4d2e0caf821c9; _m_h5_tk=19f6670c96264b8c350fc2e313f6e503_1606131576373; _m_h5_tk_enc=5d100e34a0be74e320f730d8ecf077d0; res=scroll%3A1519*5962-client%3A1519*722-offset%3A1519*5962-screen%3A1536*864; pnm_cku822=098%23E1hvYvvUvbpvUpCkvvvvvjiWP2dhAjlnP2MwAjD2PmPh0jYVnLLUzjEvnLFWzj1nR9vCvvOvCvvvphvUvpCWvjFyvva4YExrQ8g7EcqvaNoxdX%2BaWXxr1jZcR2xVI4mxfBkK4Z7xfwBlHs9lBuV918p7%2B3%2BuafFClfy64Hky%2BExre8tKjrcnI4vODb9Cvm9vvvvvphvvvvvvvQCvpv3bvvv2vhCv2UhvvvWvphvWgvvvvQCvpvs9uvhvmvvv9bu6EX%2BIkvhvC99vvOCgLQgCvvpvvPMMRvhvCvvvvvmevpvhvvmv99%3D%3D; tfstk=cy1GB3AUtOJ6JTJhF5O1y48hvuEdZYywfs5FT6o3rJtMJU1FiOHEURgT-hIGMx1..; l=eBEXfTSIOAXn7QnQXOfZhurza779OIRAguPzaNbMiOCP_PCp5aSdWZ7iuVL9CnGVh652R37kyyKBBeYBqIYpBkymtBALu-Dmn; isg=BDQ0Z_Im19zDlEM93twF5iloBfKmDVj3UHK38c6V4b9COdSD9hw9hi_7vXHhwZBP'
}
r = requests.get(url=url, headers=headers)
html = etree.HTML(r.content.decode('gbk'))
title = html.xpath('//*[@id="J_ItemList"]/div/div/p[2]/a/text()')
# 列表去换行符
title = [x.strip() for x in title]
price = html.xpath('//*[@id="J_ItemList"]/div/div/p[1]/em/text()')
month_turnover = html.xpath('//*[@id="J_ItemList"]/div/div/p[3]/span[1]/em/text()')
reply = html.xpath('//*[@id="J_ItemList"]/div/div/p[3]/span[2]/a/text()')
print(title, price, month_turnover, reply)
for i in range(0, 60):
    info = title[i], price[i]+'RMB', month_turnover[i], reply[i]
    g_csv.writerow(info)
